// pages/feedback/feedback.js
var Constant = require('../../utils/constant.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    opinionType:[
      {"id": "0","describe":"功能异常：功能故障或不可用"},
      {"id": "1", "describe": "产品建议：用的不爽我有建议"},
      {"id": "2", "describe": "安全问题：密码、隐私、欺诈等" },
      { "id": "3", "describe": "其他问题" }
      ],
    msgDescribe:'',
    msgCode:'',
    imgLists:[], //选择图片
    base64imgString:'', //拼接string
    // base64imgList : [], //转成base64
    contentCount: 0, //正文字数
    imageCount:0,//选择相片数
    isfouce: true    ,  // 切换
    textareaVal: ''    // textarea的输入值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /*
    * 点击切换
    **/
  isfouce: function () {
    this.setData({
      isfouce: false
    })
  },

  /*
  * 失焦事件
  **/
  textarea: function (e) {
    // console.log("输入",e.detail.value)
    this.setData({
      isfouce: true,
      textareaVal: e.detail.value,
      contentCount: e.detail.value.length
    })
    
  },
  //选择事件
  radioChange:function(e){
    const idx = e.detail.value
    this.data.msgDescribe = this.data.opinionType[idx].describe;
    this.data.msgCode = this.data.opinionType[idx].id;
    
    // this.data.msgId = this.data.opinionType
    // console.log("选择了",this.data.msgCode)
    // console.log("选择了", this.data.msgDescribe)
  },

  //添加图片
  bind_addImage:function(e){
    var that =this;
    wx.chooseImage({
      count:3,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: function(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const images = that.data.imgLists.concat(res.tempFilePaths)
        that.data.imgLists = images.length <= 3 ? images : images.slice(0, 3) 

        that.setData({
          imgLists: that.data.imgLists,
          imageCount:that.data.imgLists.length
        })

        //编码图片
        for(var i=0;i<that.data.imgLists.length;i++){
          picToBase64(that.data.imgLists[i],i,that)
        } 

      },
    })
  },
  //预览选择的图片
  handleImagePreview:function(e){
    const idx = e.target.dataset.idx
    const images = this.data.imgLists
    if(images.length == 0) return
    wx.previewImage({
      current: images[idx],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },

  //移除照片
  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.imgLists.splice(idx, 1)
    this.setData({
      imgLists: this.data.imgLists,
      imageCount: this.data.imgLists.length
    })
  },

  upload_feedback: function(e){
    
    var that = this;
    // console.log(that.data.textareaVal)
    if (that.data.msgDescribe == ""){
      wx.showToast({
        title: '请选择反馈类型',
        icon: 'none'
      })
      return;
    }else if (that.data.textareaVal == ""){
      wx.showToast({
        title: '请填写反馈信息',
        icon: 'none'
      })
      return;
    } else if (that.data.textareaVal.length < 10){
      wx.showToast({
        title: '反馈信息不能少于10个字',
        icon: 'none'
      })
      return;
    } else if (that.data.textareaVal.length > 500) {
      wx.showToast({
        title: '反馈信息不能多于500个字',
        icon: 'none'
      })
      return;
    }

    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: Constant.BASEURL + Constant.FEED_BACK,
      method: "POST",
      data: { "wechatname": app.globalData.userInfo.nickName
      ,"wechatphoto": app.globalData.userInfo.avatarUrl
      ,"messige":  that.data.textareaVal
      ,"openid": app.globalData.openid,
      "msgphoto":that.data.base64imgString,
      "msgcode":that.data.msgCode,
      "msgdescribe":that.data.msgDescribe},
      success: function (res) {
        // console.log(res.data)
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '反馈成功！',
          showCancel: false,
          success:function(res){
              wx.navigateBack({
                delta: -1,
              })
          }
        })
        
      },
      fail: function (err) {
        // console.log(err.errMsg)
        wx.hideLoading();
        wx.showToast({
          title: '系统错误，请重试',
          icon: 'none'
        })
      }
    })
  }

})

function picToBase64(path,index, that) {
  let manager = wx.getFileSystemManager();
  manager.readFile({
    filePath: path,
    encoding: 'base64',
    success: function (res) {
      that.data.base64imgString += res.data +',';
      // console.log("编码base64成功", that.data.base64imgString)
    },
    fail: function (res) {
      // console.log("编码失败，", res)
    }
  })
}